package com.gancp.controller;

import com.gancp.common.Constans;
import com.gancp.controller.BaseController.BaseController;
import com.gancp.domain.MyBlog;
import com.gancp.service.MyBlogBiz;
import com.gancp.utills.PageHelperUtils;
import com.github.pagehelper.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Author gancp
 * @Date 时间被黑洞吸走了
 * @Descripition 我的博客
 **/
@Controller
@RequestMapping("myBlog")
public class MyBlogController extends BaseController {
    private Logger log = LoggerFactory.getLogger( MyBlogController.class );
    @Autowired
    private MyBlogBiz myBlogBiz;

    @RequestMapping("/writeBlog")
    public String gotoPage(HttpServletRequest request) {
        log.info( getUserName( request ) + "访问了我的博客页面！" );
        return "blog/writeBlog";
    }


    @RequestMapping("/writeBlog/saveBlog")
    public @ResponseBody
    String saveBlog(HttpServletRequest request) {
        MyBlog blog = new MyBlog();
        String title = request.getParameter( "title" );
        String summary = request.getParameter( "summary" );
        String content = request.getParameter( "content" );
        blog.setTitle( title );
        blog.setSummary( summary );
        blog.setContent( content );
        blog.setCreateTime( new Date() );
        blog.setUpdateTime( new Date() );
        blog.setUserid( getUserId( request ) );
        long result = myBlogBiz.insertAndGetId( blog );
        log.info( getUserName( request ) + "提交了一篇文章" );
        return String.valueOf( blog.getBlogId() );
    }

    @RequestMapping(value = "/showMyBlog", method = RequestMethod.GET)
    public ModelAndView showMyBlog(@RequestParam long titleId, HttpServletRequest request) {
        log.info( getUserName( request ) + "查看了" + titleId + "文章！" );
        MyBlog myBlog = myBlogBiz.selectByPrimaryKey( titleId );
        ModelAndView modelAndView = new ModelAndView( "blog/showMyBlog" );
        modelAndView.addObject( "myBlog", myBlog );
        return modelAndView;
    }

    @RequestMapping("/blogList")
    public ModelAndView gotoBlogList(HttpServletRequest request) {
        log.info( getUserName( request ) + "访问了博客列表界面" );
        PageHelperUtils.execute( request );
        List<Map<String, Object>> result = myBlogBiz.queryBlogPageByParam( new MyBlog() );
        PageInfo<Map<String, Object>> pageInfo = new PageInfo( result );
        ModelAndView modelAndView = new ModelAndView( "blog/blogList" );
        modelAndView.addObject( Constans.PAGE_INFO, pageInfo );
        return modelAndView;
    }
}
